package org.zizi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateUtil
{
	private static Logger log = Logger.getLogger(DateUtil.class);
	
	/** 某年的起始时间 */
	public static Date getYearBeginTime(int year)
	{
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		
		return c.getTime();
	}
	
	/** 某年的结束时间 */
	public static Date getYearEndTime(int year)
	{
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year + 1);
		c.add(Calendar.MILLISECOND, -1);
		
		return c.getTime();
	}
	
	/** 某月的起始时间：注，月计数为1-12 */
	public static Date getMonthBeginTime(int year, int month)
	{
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		
		return c.getTime();
	}
	
	/** 某月的结束时间：注，月计数为1-12 */
	public static Date getMonthEndTime(int year, int month)
	{
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.add(Calendar.MILLISECOND, -1);
		
		return c.getTime();
	}
	
	/** 某天的起始时间 */
	public static Date getDateBeginTime(int year, int month, int date)
	{
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DATE, date);
		
		return c.getTime();
	}
	
	/** 某天的结束时间 */
	public static Date getDateEndTime(int year, int month, int date)
	{
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DATE, date + 1);
		c.add(Calendar.MILLISECOND, -1);
		
		return c.getTime();
	}
	
	public static int getYear(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		return c.get(Calendar.YEAR);
	}
	
	/** 月份为1-12 */
	public static int getMonth(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		return c.get(Calendar.MONTH) + 1;
	}
	
	public static int getDate(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		return c.get(Calendar.DATE);
	}
	
	public static String format(Date date)
	{
		return format(date, "yyyy-MM-dd");
	}
	
	public static String format(Date date, String pattern)
	{
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	public static Date parse(String source, String pattern)
	{
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try
		{
			return format.parse(source);
		}
		catch (ParseException e)
		{
			log.warn("无法转换为日期：" + source + "，转换格式为" + pattern);
			return null;
		}
	}
	
	public static String string(int seconds)
	{
		if (seconds <= 0) return null;
		
		int day = 86400;	//一天
		int hour = 3600;	//一小时
		int minute = 60;	//一分钟
		
		int d = seconds / day;			//天数
		seconds = seconds - d * day;
		
		int h = seconds / hour;			//小时
		seconds = seconds - h * hour;
		
		int m = seconds / minute;		//分钟
		seconds = seconds - m * minute;
		
		String sz = "";
		if (d > 0) sz += d + "天";
		if (h > 0) sz += h + "小时";
		if (m > 0) sz += m + "分钟";
		if (seconds > 0) sz += seconds + "秒";
		
		return sz;
	}
}
